package es.ats.codington.festival.daos;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.interfaces.daos.IEventDAO;
import es.ats.codington.festival.mappers.EventMapper;
import es.ats.codington.festival.mappers.EventSignUpMapper;
import es.ats.codington.festival.utils.EventDbQuery;
import es.ats.codington.festival.utils.FERSDataConnection;
import es.ats.codington.festival.utils.JSONQuery;

/**
 * The Class EventDAOImpl.
 */
public class EventDAOImpl implements IEventDAO{
	
	
	/** The address. */
	private static String address = "src/es/ats/codington/festival/utils/DBJson.json";
	private JdbcTemplate jdbcTemplateObject;
	private Properties queryProperties;

	public EventDAOImpl(JdbcTemplate jdbcTemplate, Properties queryProperties) {
		this.jdbcTemplateObject = jdbcTemplate;
		this.queryProperties = queryProperties;
	}

	
	public int registerEventByVisitor(int idVisitor, int idEvent){
		return idEvent;
		
		
	}
	

	@Override
	public ArrayList<Event> obtainAllEvent() throws Exception {
		boolean rs = false;
		ArrayList<Event> ev = new ArrayList<Event>();
		try{	
			
			
    		List<Event> id_eventos =  jdbcTemplateObject.query(queryProperties.getProperty("all_events"),new PreparedStatementSetter(){
				@Override
				public void setValues(PreparedStatement statement)
						throws SQLException {
				}
			}, new EventMapper());
    		
    		for (int i=0; i<id_eventos.size(); i++){
    			ev.add(id_eventos.get(i));
    		}
    		
			
		

	
		return ev;
		} catch(Exception ex) {
			System.out.println(ex.getMessage());
			return ev;
		}	
		
	}
	
	
	//DELETE EVENT	  
	/**
	 * Delete event dao.
	 *
	 * @param con the con
	 * @param idEvent the id event
	 * @return the int
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.daos.IEventDAO#deleteEventDAO(java.sql.Connection, int)
	 */
	@Override
	public int deleteEventDAO(final int idEvent)throws Exception{
					ArrayList<Event> ev = new ArrayList<Event>();
					try{					
			    		int id_eventos =  jdbcTemplateObject.update(queryProperties.getProperty("deleteEventSignUp_Id"),new PreparedStatementSetter(){
							@Override
							public void setValues(PreparedStatement statement)
									throws SQLException {
								statement.setInt(1, idEvent);
							}
							});
			    		
										
				    		 int id_eventos2 =  jdbcTemplateObject.update(queryProperties.getProperty("deleteEvent_Id"),new PreparedStatementSetter(){
								@Override
								public void setValues(PreparedStatement statement)
										throws SQLException {
									statement.setInt(1, idEvent);
								}
								});
							id_eventos = id_eventos + id_eventos2;
							return id_eventos;
					}catch (Exception e){
						System.out.println(e.getMessage());
						return 0;
					}
	

	  }
	@Override
	public Event updateSelectEventDAO(final int idEvent) throws Exception{
		 Event event = new Event();
		List<Event> eventos =  jdbcTemplateObject.query(queryProperties.getProperty("get_events_idV"),new PreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement statement)
					throws SQLException {

				statement.setInt(1, idEvent);
				
			}
		}, new EventMapper());
		
		event = eventos.get(0);
		
		return event;
		
	}
	
	
	
	@Override
	public int updateEventDAO(final Event event) throws Exception {

			int eventos =  jdbcTemplateObject.update(queryProperties.getProperty("updateEvent"),new PreparedStatementSetter(){
				@Override
				public void setValues(PreparedStatement statement)
						throws SQLException {

					statement.setString(1, event.getName());
					statement.setString(2, event.getDescription());
					statement.setInt(3, event.getDuration());
					statement.setString(4, event.getTypeEvent());
					statement.setInt(5, event.getCapacity());
					statement.setInt(6, event.getIdPlace());	
					statement.setInt(7, event.getId());
				}
				
			});			
			return eventos;
	}
	
	
	

	@Override
	public int insertEventDAO(Connection con, Event insertevent)
			throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}





	@Override
	public boolean checkEvent(int idPlace) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean showAllEvent() {
		// TODO Auto-generated method stub
		return false;
	}

	

	//INSERT EVENT
	/**
	 * Insert event dao.
	 *
	 * @param con the con
	 * @param insertevent the insertevent
	 * @return the int
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.daos.IEventDAO#insertEventDAO(java.sql.Connection, es.ats.codington.festival.domain.Event)
	 */
	/*public int insertEventDAO(Connection con, Event insertevent)throws Exception{		  
		  int rows=0;		  
			try {				
			    String query = "insert into Event (Name,Description,Duration,EventType,Capacity,PlaceId) values(?,?,?,?,?,?);";						    
			    rows = EventDbQuery.insEventTable(con,insertevent,query);			  			
			} catch(SQLException ex) {
				System.out.println(ex.getMessage());
			}			
			return rows;
	  }
	  */
	  

	  
	// UPDATE EVENT
	/**
	 * Update event dao.
	 *
	 * @param con the con
	 * @param event the event
	 * @return the int
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.daos.IEventDAO#updateEventDAO(java.sql.Connection, es.ats.codington.festival.domain.Event)
	 */
/*	public int updateEventDAO(Connection con, Event event)throws Exception{
		  int rows=0;
		  EventDbQuery executeQuery = new EventDbQuery();
		  
		  try{		
			    String query = "update Event set Name=?, Description=?, Duration=?, EventType=?,Capacity=?,PlaceId=? where idEvent=?;";
			    
			  //Creating a PreparedStatement with a query 
				PreparedStatement pStmt = con.prepareStatement(query);
			    
			    rows = executeQuery.upEventTable(event, con, pStmt);
			
			}catch (Exception e){
				System.out.println(e.getMessage());
			}
		  
		  return rows;

	  }*/
	  
	/**
	 * Show all event.
	 *
	 * @return true, if successful
	 * @see es.ats.codington.festival.interfaces.daos.IEventDAO#showAllEvent()
	 */
  /*	public boolean showAllEvent(){
		  boolean show = false;
		  try{
				//Obtaining a connection
				Connection con = FERSDataConnection.getConnection();
				
				JSONQuery reader = new JSONQuery(address);

			    String query = reader.readString("Event_Show");
			    
				if(con != null)
				{
					System.out.println("Connection successfully established!");
				}
				
				//Creating a PreparedStatement with a query "select * from Event" 
				PreparedStatement pStmt = con.prepareStatement(query);
				
				//Executing this query using executeQuery()
				ResultSet rs = pStmt.executeQuery();
				
				show=true;
			/*	
				//Process the resultSet and print all records on console
				System.out.println("ID		Name  		Description				Duration		Event type 		Capacity");
				while(rs.next())
				{
					System.out.println(rs.getInt(1)+"		"+ rs.getString(2) + "					" + rs.getString(3)+"		"+ rs.getInt(4)+"			"+ rs.getString(5)+"		"+rs.getInt(6));
					
				}*/
				//Closing the connection
			/*	con.close();
				
			}catch (Exception e){
				System.out.println(e.getMessage());
			}
		  
		  return show;
	  }*/
	  
	/**
  	 * Search eventby id dao.
  	 *
  	 * @param idEvent the id event
  	 * @param con the con
  	 * @return the event
  	 */
 /* 	public Event searchEventbyIdDAO(int idEvent, Connection con){ 
		  Event event = new Event();				  
			try {				
				String query = "select * from Event where idEvent = ?;";	    
			    event = EventDbQuery.searchById(con,query,idEvent);			
			} catch(Exception ex) {
				System.out.println(ex.getMessage());
			}			
			return event;
	  }	  
	  */
	  
	/**
	 * Check event.
	 *
	 * @param idPlace the id place
	 * @return true, if successful
	 * @see es.ats.codington.festival.interfaces.daos.IEventDAO#checkEvent(int)
	 */
  /*	public boolean checkEvent(int idPlace){
		  boolean check = false;
		  int rows=0;
		  
		  try {
				Connection con = FERSDataConnection.getConnection();
				
				
				if(con != null)
				{
				System.out.println("Connection successfully established!");
				}
				
				//Creating a PreparedStatement with a query "insert into Employee values(?,?,?)" 
				PreparedStatement place = con.prepareStatement("select * from Place where PlaceId = ?");				
				
							
				//Replacing ? with actual value 
				place.setInt(1, idPlace);
				
				//Setting autoCommit to false 
				con.setAutoCommit(false);
				
				//Executing this query using executeUpdate()
				rows = place.executeUpdate();
				
				if (rows != 0 ){
					check = true;
				}
				
				//Committing
				con.commit();	
				
						
				//Closing the connection
				con.close();
				} catch(SQLException ex) {
					System.out.println(ex.getMessage());
				}
		  
		  
		  return check;
	  }
*/

	/**
	 * Obtain all event dao.
	 *
	 * @param con the con
	 * @return the array list
	 */
/*	public ArrayList<Event> obtainAllEventDAO(Connection con) {
	    String chain = "select * from event;";
	    ArrayList<Event> eve = new ArrayList<Event>();
	    
	    System.out.println("SQL->"+chain);	    
	    	eve = EventDbQuery.obtainAllEvent(con, chain);
	    	
		return eve;		
	}
*/


}
